package com.htandroid.htandroid.webservice;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;

import android.util.Xml;

import com.htandroid.htandroid.entity.UffjOptions;
import com.htandroid.htandroid.entity.UffjqgdList;

public class XMLUffjOptions {

    /**
     * 解析评论xml信息
     *
     * @param result
     * @return
     */
    public static ArrayList<UffjOptions> paraseCommentInfors(String result) {
        ArrayList<UffjOptions> list = new ArrayList<UffjOptions>();
        //字符转换成字节流
        InputStream inputStream = new ByteArrayInputStream(result.getBytes());
        XmlPullParser parser = Xml.newPullParser();
        String aa = "";
        try {
            parser.setInput(inputStream, "UTF-8");
            int eventType = parser.getEventType();
            UffjOptions info = new UffjOptions();

            while (eventType != XmlPullParser.END_DOCUMENT) {
                switch (eventType) {
                    case XmlPullParser.START_DOCUMENT:// 文档开始事件,可以进行数据初始化处理
                        break;
                    case XmlPullParser.START_TAG:// 开始元素事件
                        String name = parser.getName();
                        if (name.equalsIgnoreCase("UffjOptions")) {
                            info = new UffjOptions();
                        } else {
                            eventType = parser.next();
                            if (name.equalsIgnoreCase("ID")) {
                                info.setID(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("dd_num")) {
                                info.setDd_num(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("qgd_num")) {
                                info.setQgd_num(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("shd_num")) {
                                info.setShd_num(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("yffp_num")) {
                                info.setYffp_num(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("shd_dep")) {
                                info.setShd_dep(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("ap_ccode_c")) {
                                info.setAp_ccode_c(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("ap_ccode_d")) {
                                info.setAp_ccode_d(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("shd_rdincode")) {
                                info.setShd_rdincode(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("zbd_rdincode")) {
                                info.setZbd_rdincode(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("zbd_rdoutcode")) {
                                info.setZbd_rdoutcode(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("sh_check_close")) {
                                info.setSh_check_close(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("price")) {
                                info.setPrice(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("sh_rd_check")) {
                                info.setSh_rd_check(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("row_num")) {
                                info.setRow_num(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("print_unit")) {
                                info.setPrint_unit(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("print_taoda")) {
                                info.setPrint_taoda(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("qgd_dd_check")) {
                                info.setQgd_dd_check(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("zbd_cwhcode")) {
                                info.setZbd_cwhcode(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("rd_rdoutcode")) {
                                info.setRd_rdoutcode(string2null(parser.getText()));
                            }
                        }
                        break;
                    case XmlPullParser.END_TAG:// 结束元素事件
                        if (parser.getName().equalsIgnoreCase("UffjOptions")) {
                            list.add(info);
                            info = null;
                        }
                        break;
                }
                eventType = parser.next();
                if (eventType == 3) {
                    if (aa.equals("")) {
                        aa = "1";
                    } else if (aa.equals("1")) {
                        aa = "2";
                    } else {
                        break;
                    }
                } else {
                    aa = "";
                }
            }
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    //函数作用是把null 转为空
    public static String string2null(String str) {
        String rtn = "";
        if (str == null || str.equals("null") || str.equals("anyType{}")) {
            rtn = "";
        } else {
            rtn = str;
        }
        return rtn;
    }

    public static String convertStreamToString(InputStream is) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "/n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
}  
